package com.forum.controller;

import com.forum.common.api.CommonResult;
import com.forum.model.vo.PostVO;
import com.forum.service.FavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "收藏接口")
@RestController
@RequiredArgsConstructor
public class FavoriteController {
    
    private final FavoriteService favoriteService;
    
    @Operation(summary = "收藏帖子")
    @PostMapping("/post/{id}/favorite")
    public CommonResult<Boolean> favoritePost(@PathVariable Long id, @RequestParam(required = false) Long userId) {
        if (userId == null) {
            return CommonResult.unauthorized("请先登录");
        }
        boolean result = favoriteService.favorite(userId, id);
        return CommonResult.success(result);
    }
    
    @Operation(summary = "取消收藏帖子")
    @DeleteMapping("/post/{id}/favorite")
    public CommonResult<Boolean> unfavoritePost(@PathVariable Long id, @RequestParam(required = false) Long userId) {
        if (userId == null) {
            return CommonResult.unauthorized("请先登录");
        }
        boolean result = favoriteService.favorite(userId, id);
        return CommonResult.success(!result);
    }
    
    @Operation(summary = "获取帖子收藏状态")
    @GetMapping("/post/{id}/favorite/status")
    public CommonResult<Boolean> getPostFavoriteStatus(@PathVariable Long id, @RequestParam(required = false) Long userId) {
        if (userId == null) {
            return CommonResult.success(false);
        }
        boolean hasFavorited = favoriteService.hasFavorited(userId, id);
        return CommonResult.success(hasFavorited);
    }
    
    @Operation(summary = "获取用户收藏的帖子列表")
    @GetMapping("/user/{userId}/favorites")
    public CommonResult<List<PostVO>> getUserFavorites(@PathVariable Long userId) {
        List<PostVO> favorites = favoriteService.getUserFavorites(userId);
        return CommonResult.success(favorites);
    }
} 